<template>
  <div class="home_container">
    <van-nav-bar class="search"
                 fixed>
      <van-button class="search-btn"
                  slot="title"
                  round
                  size="small"
                  to="/search">
        <i class="iconfont icon-search"> 搜索</i>
      </van-button>
    </van-nav-bar>

    <van-tabs class="tabs"
              scroll
              v-model="active"
              animated
              swipeable>
      <van-tab class="tab"
               :title="channel.name"
               v-for="channel in channels"
               :key="channel.id">
        <article-list :channel="channel" />
      </van-tab>

      <div slot="nav-right"
           class="placeholder"></div>
      <div slot="nav-right"
           @click="isShowPopup= true"
           class="menu">
        <i class="iconfont icon-menu2"></i>
      </div>

    </van-tabs>

    <van-popup v-model="isShowPopup"
               closeable
               close-icon-position="top-left"
               position="bottom"
               :style="{height:'100%'}">
      <channel-edit :my-channes="channels"
                    :current="active"
                    @current-click="currentClick"></channel-edit>
    </van-popup>

  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getToken } from '../../utils/storage.js'

export default {
  components: { ArticleList, ChannelEdit },
  name: 'HomeIndex',
  data () {
    return {
      active: 0,
      channels: [],
      isShowPopup: false
    }
  },
  created () {
    this.loadChannels()
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    async loadChannels () {
      try {
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          const localChannels = getToken('TOUTIAO_CHANNELS')
          if (localChannels) {
            channels = localChannels
          } else {
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道数据失败！')
      }
    },
    currentClick (id, isShowPopup = true) {
      this.active = id
      this.isShowPopup = false
    }
  }
}
</script>

<style lang="less" scoped>
.search {
  box-shadow: 3px 5px 5px 3px rgba(0, 0, 0, 0.1);
  background-color: #40b783;
  z-index: 2;
  .search-btn {
    width: 500px;
    .iconfont {
      font-size: 28px;
      color: #9f9f9f;
    }
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
}
/deep/ .tabs {
  .van-tab {
    min-width: 200px;
    border-right: 1px solid #f0f0f0;
    border-bottom: 1px solid #f0f0f0;
  }
  .van-tabs__line {
    background-color: #40b783;
    z-index: 1;
  }
}
.placeholder {
  flex-shrink: 0;
  width: 66px;
  height: 82px;
}
.menu {
  position: fixed;
  right: 0;
  width: 66px;
  height: 86px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-left: 1px solid rgb(239, 239, 239);
  i {
    font-size: 30px;
    color: #40b783;
  }
}
.home_container {
  padding-bottom: 79px;
  padding-top: 180px;
}
/deep/.van-tabs__wrap {
  position: fixed;
  top: 92px;
  z-index: 1;
  left: 0;
  right: 0;
}
</style>
